//规则, 组件名字驼峰, 元素标签中划线分割, 两者映射缺一不可.
angular.module('app').component('winitIssueNeed', {
    templateUrl: getTemplateUrl('matrix/component/tpl/winitIssueNeed.html'),
    bindings: {
        userList: '<',
        issueList: '<'
    },
    controller: function($http, $rootScope, $scope, $element, $attrs, lodash) {
        var ctrl = this;
        ctrl.info = {
            issue: true
        };

        if (ctrl.issueList == undefined) {
            ctrl.issueList = [];
        }
        ctrl.canOperate = !$rootScope.isUserCanOperateByRole('pdm');
        // 初始化用户
        initUserItem();
        initTagGroup();

        ctrl.$onChanges = function() {
            if (ctrl.issueList.length > 0) {
                angular.forEach(ctrl.issueList, function(issue) {
                    issue.sltTagGroup = issue.groupTag;
                    issue.sltUser = { "name": issue.user, "crtname": issue.user };
                    var sltAccount = lodash.findWhere(ctrl.userList, { "account": issue.toAccount });
                    issue.sltToAccount = sltAccount || "";
                });
            }
        };

        function initUserItem() {
            angular.forEach(issueUser, function(user) {
                user.crtname = user.name;
            });
            ctrl.issueUser = issueUser;
        };

        function initTagGroup() {
            angular.forEach(tagGroup, function(tagGroup) {
                tagGroup.crtname = tagGroup.name;
            });
            ctrl.tagGroup = tagGroup;
        }

        // 显示modal时，默认加载一条数据
        function getIssueCode() {
            var url = matrix_product_url + "/issue/getIssueNumber";
            $http.get(url).success(function(result) {
                if (result.code == "0") {
                    if (result.data) {
                        var initData = { "number": result.data, "user": "卖家", "sltTagGroup": "", "sltUser": { "name": "卖家", "crtname": "卖家" }, "action": "", "creatorName": getUserName(), "description": "", "toAccount": getUserAccount(), "sltToAccount": { "account": getUserAccount(), "realname": getUserName(), "crtname": getUserName(), "id": getUserId() } };
                        ctrl.issueList.push(initData);
                    } else {
                        $rootScope.toasterErrorInfo("获取IssueCode失败！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        };

        ctrl.clickAddTr = function() {
            getIssueCode();
        }

        // 删除行
        ctrl.clickDeleteBtn = function(info) {
            var confirmParams = {
                "titleData": "提示",
                "bodyData": "是否删除此需求吗？"
            };
            $rootScope.confirmWindow(confirmParams, function(data) {
                if (data) {
                    if(!info.id) {
                        lodash.remove(ctrl.issueList, function(reqData) {
                            return reqData.number == info.number;
                        });
                        $rootScope.toasterSuccessInfo("删除成功！");
                    } else {
                        var url = matrix_product_url + "/issue/deleteIssue?issueId=" + info.id;
                        $http.get(url).success(function(result) {
                            if (result.code == "0") {
                                lodash.remove(ctrl.issueList, function(reqData) {
                                    return reqData.number == info.number;
                                });
                                $rootScope.toasterSuccessInfo("删除成功！");
                            } else {
                                $rootScope.toasterErrorInfo(result.desc);
                            }
                        });
                    }
                }
            })
        };
    }
})